﻿Imports System.Data.OleDb


Public Class HotelDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            If Request.Params("ID") = "" Then Response.Redirect("FoodList.aspx")
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("FoodConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM comments WHERE Food_FKID=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView2.DataSource = dataReader
            GridView2.DataBind()

            Dim foodViewedSql As String = "INSERT INTO food_Viewed(FoodUser,Food_fkid) VALUES (@f1,@f2)"
            Dim cmd2 As OleDbCommand = New OleDbCommand(foodViewedSql, oleDbConn)
            cmd2.CommandType = CommandType.Text
            cmd2.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd2.Parameters.AddWithValue("@f1", Request.Params("ID"))
            cmd2.ExecuteNonQuery()

        End If
    End Sub

    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting

    End Sub
    Protected Sub btn_AddComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("FoodConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into comments(Author,Comment,Title,Food_FKID) Values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        'Fill GridView2
        Dim fillComments As String = "SELECT * FROM comments WHERE Food_FKID=@f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader = fillCommentsCmd.ExecuteReader()
        GridView2.DataSource = dataReader
        GridView2.DataBind()

        tb_Comment.Text = ""
        tb_Title.Text = ""
        'Response.Redirect("confirmation_comment.aspx")
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub

    Protected Function getRating(ByVal Stars As Integer)
        Dim retval As String
        retval = ""
        For i = 1 To Stars
            retval = retval + "*"
        Next
        Return retval
    End Function

    Protected Sub btn_addTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_addTag.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("FoodConnectionString").ConnectionString)
        Dim SqlString As String = "insert into junction(Food_FKID,Tag_FKID) Values (@f1,@f2)"
        oleDbConn.Open()

        For Each cb As ListItem In cb_tags.Items
            If cb.Selected Then
                'here is where we add the join to the table of tags and foods
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", cb.Value)
        cmd.ExecuteNonQuery()
            End If
        Next

        If TextBox1.Text = "" Then Return

        Dim newTags = TextBox1.Text.Split(",")
        For Each newTag In newTags
            'first add the new tag to the tag table
            Dim newTagSql As String = "insert into tags(Tag) values (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'then find the id of the new tag
            Dim getLastIDCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID from Tags ORDER BY ID DESC", oleDbConn)
            Dim dr = getLastIDCmd.ExecuteReader()
            dr.Read()
            Dim lastID = dr.GetValue(0)

            'associate this last id with the food id
            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            newcmd.Parameters.AddWithValue("@f2", lastID)
            newcmd.ExecuteNonQuery()
        Next
    End Sub

    Protected Sub btn_SaveRating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SaveRating.Click

        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("FoodConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim delRatingSql As String = "Delete from ratings WHERE Food_FKID=@f1 AND FoodUser=qf2"
        Dim delRatingCmd As New OleDbCommand(delRatingSql, oleDbConn)
        delRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        delRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        delRatingCmd.ExecuteNonQuery()

        Dim addRatingSql As String = "Insert into ratings(Food_FKID,FoodUser,Rating) Values (@f1,@f2,@f3)"
        Dim addRatingCmd As New OleDbCommand(addRatingSql, oleDbConn)
        addRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        addRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addRatingCmd.Parameters.AddWithValue("@f3", Rating1.CurrentRating)
        addRatingCmd.ExecuteNonQuery()

        Label1.Text = "Thank you for adding your rating!"

    End Sub


End Class